import http from './http.js'
const userPermissions = new Set<string>();
export async function getPermissionsFromRemote() : Promise<string[]>{
	// 从后台获取权限
	// let resData = await http.post('/gateway/PLATFORM-SUPPORT/v1/pv/permission/register');
	// console.log('permission Data:', resData)
	// mock
	const arr = ["010100", "02010100", "02010101", "02010102", "02010103", "02010200", "02010201", "02010202", "02010203", "02010251", "020200", "020201", "020300", "020301", "020302", "020352", "020400", "030100", "030101", "030102", "030104", "04010100", "04010101", "04010102", "04010104", "04010154", "04010200", "04010201", "04010202", "04010204", "04010254", "04010300", "04010305", "04020100", "04020101", "04020102", "04020104", "04020105", "04020200", "04020201", "04020202", "04020204", "04020205", "04020300", "04020301", "04020302", "04020304", "04020305", "04020400", "04020401",

		"04020402", "04020403", "04020404", "04020405", "04030100", "04030101", "04030102", "04030103", "050100", "050101", "050102", "050104", "050107", "050200", "050205", "060100", "060108", "060102", "060200", "060201", "060202", "060203", "060207", "060300", "060307", "060305", "070100", "070101", "070102", "070104", "070115", "070200", "070201", "070202", "070204", "070205", "070207", "070300", "070301", "070302", "070304", "07040100", "07040101", "07040102", "07040104", "07040150", "07040200", "07040204", "07040205", "07040207",

		"070500", "070503", "0706010100", "0706010101", "0706010102", "0706010103", "0706010105", "0706010200", "0706010201", "0706010202", "0706020100", "0706020101", "0706020102", "0706020103", "0706020104", "0706020100", "0706020101", "0706020102", "070700", "070701", "070704", "070707", "0800", "0801", "0802", "0804", "090100", "090105", "090109", "100100", "100102", "100153", "10020100", "10020159", "10020160", "10020200", "10030100", "10030155", "10030200", "10030256", "100400", "100402", "100457", "100404", "10050100", "200100", "200200", "200300", "200400", "200401", "200402", "200403", "200406", "200408", "200409", "200411", "200412", "200500", "200600"
	]

	return arr
}

export async function install (): Promise<Set<string>> {
    let permissions= await getPermissionsFromRemote();
    for (let code of permissions) {
        if (code.endsWith('00')) {
            let moduleCode = code.substring(0, code.length - 2);
            while (moduleCode && moduleCode.length > 0) {
                userPermissions.add(moduleCode);
                moduleCode = moduleCode.substring(0, moduleCode.length - 2);
            }
        }
    }
    return userPermissions
}
export function hasPermission(actionCode:string):boolean{
    return userPermissions.has(actionCode)
}
export function getUserPermission():Set<string>{
    return userPermissions;
}
// 统一action code; 仅作参考
	// '**00': '查看',
	// '**01': '新增/添加**',
	// '**02': '编辑（修改备注）',
	// '**03': '删除',
	// '**04': '上/下架（含批量）',
	// '**05': '排序（上/下移/置顶/取消置顶）',
	// '**06': '',
	// '**07': '推荐/取消推荐',
	// '**08': '导入',
	// '**09': '导出',

	// '**50': '添加帖子',
	// '**51': '初始化密码',
	// '**52': '新增/取消一键邀请',
	// '**53': '发送(单发/群发)',
	// '**54': '关联',
	// '**55': '派稿(含批量)',
	// '**56': '审核',
	// '**57': '结束任务',
	// '**58': '解除授权',
	// '**59': '查看收益',
	// '**60': '查看银行卡',